package com.liugh.service.zhongche.Imp;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.liugh.entity.standard.StandardPage;
import com.liugh.mapper.zhongche.StandardItemMapper;
import com.liugh.mapper.zhongche.StandardPageMapper;
import com.liugh.service.IUserService;
import com.liugh.service.zhongche.IStandardPageService;
import com.liugh.util.FileUpload;
import com.liugh.util.LiughFileUtil;
import org.csource.common.MyException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

//import com.liugh.util.FileUtil;

/**
 * Created by Fatao on 2018/10/30 .
 */
@Service
@EnableTransactionManagement(proxyTargetClass = true)
public class StandPageServiceImpl extends ServiceImpl<StandardPageMapper, StandardPage> implements IStandardPageService {

    private static org.slf4j.Logger Logger = LoggerFactory.getLogger(StandPageServiceImpl.class);

    @Autowired
    private StandardPageMapper standardPageMapper;

    @Autowired
    StandardItemMapper itemMapper;

    @Autowired
    IUserService userService;

    /**
     * 根据itemId来获取所有的page .
     * @param itemId .
     * @return .
     */
    public Page<StandardPage> getItemPageList(Page page,int itemId) {
        List<StandardPage> standardPageList = standardPageMapper.selectPage(page,new EntityWrapper<StandardPage>().eq("standard_item_id", itemId));
        page.setTotal(standardPageList.size());
        page.setSize(page.getSize());
        page.setCurrent(page.getCurrent());
        return page.setRecords(standardPageList);
    }


    /**
     * 将碎片化后的图片存储到服务器
     *
     * @param list
     * @throws IOException
     * @throws MyException
     */
    @Transactional
    public void handleSegment(List<String> list, int itemId, String name,String imgPath) throws Exception {
        if (!CollectionUtil.isEmpty(list)) {
            Logger.info("开始将碎片化以后的图片上传");

            String uploadUrl;
            MultipartFile multipartFile;
            StandardPage standardPage;

            //将碎片化以后的文件上传
            for (int i = 0, j = list.size(); i < j; i++) {
                multipartFile = LiughFileUtil.fileToMulFile(FileUtil.file(list.get(i)));
                uploadUrl = FileUpload.saveFile(multipartFile);
                Logger.info("上传第 " + i + "张图片，图片存储地址为：" + uploadUrl);
//                添加到数据库
                standardPage = new StandardPage(itemId, i, name, uploadUrl);
                standardPageMapper.insert(standardPage);
            }
            FileUtil.del(imgPath);
        }
    }

}
